package com.primb.bench.common.module.repository;

import android.text.TextUtils;

import com.primb.bench.common.module.http.service.ApiClient;
import com.primb.bench.common.module.http.service.ApiService;
import com.primb.bench.project.approval.entity.ApprovalNextEntity;
import com.primb.bench.project.approval.entity.RequestNextEntity;
import com.primb.bench.project.home.entity.HttpStrEntity;
import com.primb.netlibs.converter.RxSchedulerUtils;
import com.primb.netlibs.exception.ApiException;

import java.util.HashMap;

import rx.Observable;

/**
 * Created by Chen on 2017/5/24.
 * 功能描述：
 */

public class ApprovalDetailRepository {

    public static Observable<ApprovalNextEntity> findNextApproval(String priceId, int code) {
        String goOrBack = "go";
        if (code == 1) {
            goOrBack = "back";
        }
        return ApiClient.initService(ApiService.class).findNextApproval(goOrBack, priceId)
                .map(entity -> {
                    if (!entity.isSuccess())
                        Observable.error(new ApiException(entity.getMsg()));
                    return entity;
                }).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    public static Observable<HttpStrEntity> commitAdvice(RequestNextEntity entity) {
        HashMap<String, String> map = new HashMap<>();
        map.put("priceId", entity.getPriceId());
        map.put("affairStatus", entity.getAffairStatus());
        map.put("oldProcStatus", entity.getOldProcStatus());
        map.put("newProcStatus", entity.getNewProcStatus());
        map.put("nextUsers", entity.getNextUsers());
        map.put("nextNames", entity.getNextNames());
        map.put("nextUserPhones", entity.getNextUserPhones());
        map.put("empMark", entity.getEmpMark());
        map.put("goOrBack", entity.getGoOrBack());
        if (entity.getBackReason() != null && !TextUtils.isEmpty(entity.getBackReason())) {
            map.put("sendBackReason", entity.getBackReason());
        }
        if (entity.getIsSubmit() != null && !entity.getIsSubmit().equals(""))
            map.put("isSubmit", entity.getIsSubmit());
        return ApiClient.initService(ApiService.class).commitAdvice(map).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }
}
